﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration;
using System.Configuration.Install;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Security.AccessControl;
using System.ServiceProcess;
using System.Xml;

namespace OrbitOne.Elmah.Agent
{
    [RunInstaller(true)]
    public partial class ProjectInstaller : System.Configuration.Install.Installer
    {
        public ProjectInstaller()
        {
            InitializeComponent();
        }

        public override void Install(IDictionary stateSaver)
        {
            base.Install(stateSaver);

            string targetDir = Context.Parameters["targetdir"];

            string agentName = Context.Parameters["name"];
            string description = Context.Parameters["description"];
            string url = Context.Parameters["serviceurl"];
            string logs = Context.Parameters["logsfolder"];
            string invalidlogs = Context.Parameters["invalidlogsfolder"];
            DateTime installDate = DateTime.Now;

            if (targetDir.EndsWith("\\\\"))
            {
                targetDir = targetDir.Substring(0, targetDir.Length - 1);
            }

            if (!Path.IsPathRooted(logs))
            {
                logs = Path.Combine(targetDir, logs);
                Directory.CreateDirectory(logs);
            }
            if (!Path.IsPathRooted(invalidlogs))
            {
                invalidlogs = Path.Combine(targetDir, invalidlogs);
                Directory.CreateDirectory(invalidlogs);
            }

            string exePath = targetDir + "OrbitOne.Elmah.Agent.exe";

            EventLog.WriteEntry("Elmah Agent Installer",
                                exePath + Environment.NewLine +
                                agentName + Environment.NewLine +
                                description + Environment.NewLine +
                                url + Environment.NewLine +
                                logs + Environment.NewLine +
                                invalidlogs + Environment.NewLine
                                );

            string configPath = exePath + ".config";
            var document = new XmlDocument();

            document.Load(configPath);

            var agentConfiguration = document.SelectSingleNode("configuration/AgentConfiguration");

            if (agentConfiguration != null)
            {
                agentConfiguration.Attributes["AgentName"].Value = agentName;
                agentConfiguration.Attributes["AgentDescription"].Value = description;
                agentConfiguration.Attributes["AgentCreatedOn"].Value = installDate.ToString();
                agentConfiguration.Attributes["ElmahWebServiceUrl"].Value = url;
                agentConfiguration.Attributes["ErrorXmlSourceFolder"].Value = logs;
                agentConfiguration.Attributes["InvalidLogFileFolder"].Value = invalidlogs;

                document.Save(configPath);
            }

            var controller = new ServiceController("Orbit One Elmah Agent");
            controller.Start();
        }

        private void AgentServiceProcessInstaller_AfterInstall(object sender, InstallEventArgs e)
        {

        }
    }
}
